﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using AlmaDatabase;

namespace Alma.Models
{   
    public class ProductDetailsModel
    {
        public long ID { get; set; }
        public long? ID_Section { get; set; }
        public long? ID_Shop { get; set; }
        public string Name_Shop { get; set; }
        public long ID_Cat { get; set; }
        public string Name_Cat { get; set; }
        public int? Quantity{get;set;}

        public float? Price { get; set; }
        public Criterion PriceCriterion { get; set; }

        public int Availability { get; set; }
        public string AvailabilityName { get; set; }

        public List<Product> Similars { get; set; }

        public List<ListModel> Lists { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [DisplayName("Section")]
        public string Name_Section { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [DisplayName("Nom")]
        public string Name { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        [DisplayName("Description")]
        public string Description { get; set; }

        [UIHint("ProductImage")]
        [DataType(DataType.ImageUrl)]
        public string ImgUrl { get; set; }
        public List<Tuple<string, string, string, long>> CriterionList { get; set; }
        public string reviewText { get; set; }
        public int? reviewScore { get; set; }
        public float score { get; set; }
        public List<ProductReviewModel> reviews {get;set;}

        public ProductDetailsModel()
        {
            reviews = new List<ProductReviewModel>();
          //  newReview = new ProductReviewModel();
        }

        public ProductDetailsModel(Product p)
        {
            ID = p.ID;
            ID_Section = p.ID_Section;
            ID_Shop = p.ID_Shop;
            Name = p.Name;
            Description = p.Description;
            ImgUrl = p.ImgUrl ?? "~/Content/images/unavailable.gif";
           // newReview = new ProductReviewModel(p.ID);

            using (var context = new AlmaDBEntities())
            {
                var section = from sections in context.Sections
                              where sections.ID == ID_Section
                              select sections;
                Name_Section = section.First().Name;
                var shop = from shops in context.Shops
                           where shops.ID == ID_Shop
                           select shops;
                Name_Shop = shop.First().Name;
                /*criterion*/
                CriterionList = new List<Tuple<string, string, string, long>>();
                foreach (var criterion in (from jtable in context.ProductCriterions
                                           join criteria in context.Criteria on jtable.ID_Criterion equals criteria.ID
                                               into ords
                                           where jtable.ID_Product == ID
                                           select jtable))
                {
                    CriterionList.Add(Tuple.Create(criterion.Criterion.Name, criterion.Value, criterion.Criterion.Unit, criterion.Criterion.ID));
                }
                /*reviews*/
                reviews = new List<ProductReviewModel>();
                var comments = from reviewz in context.T_Product_Reviews
                               where reviewz.ID_Product == p.ID
                               select reviewz;
                foreach (var review in comments)
                {
                    ProductReviewModel model = new ProductReviewModel();
                    model.Text = review.Text;
                    reviews.Add(model);
                }
            }
            Availability = p.Availability;
            if (Availability == 2)
                AvailabilityName = "Temporairement épuisé";
            else if (Availability == 1)
                AvailabilityName = "En stock";
            else
                AvailabilityName = "Indisponible";

        }

    }

    public class ProductListModel
    {
        public int ID { get; set; }
        public long ID_Cat { get; set; }
        public string Name_Cat { get; set; }
        public string Name { get; set; }
        public List<ProductDetailsModel> List { get; set; }

        public List<SectionCriterion> Criterions { get; set; }
    }
}
